package com.gms.service.usermanager.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gms.entity.user.User;
import com.gms.mapper.UserMapper;
import com.gms.service.usermanager.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import java.util.List;

/**
 * Service层
 *
 *
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public User checkForDuplication(String aNumber){
        return userMapper.checkForDuplication(aNumber);
    }

    @Override
    public User login(String aNumber, String passWord) {
        return userMapper.login(aNumber,passWord);
    }

    @Override
    public void register(User user){
        userMapper.register(user);
    }


    @Override
    public List<User> getAllUser() {
        return userMapper.getAllUser();
    }

    @Override
    public void update_password(User user){
        userMapper.updateById(user);
    };

    @Override
    public void updateUser(User user){
        userMapper.updateById(user);
    };

    @Override
    public void transfer(User user1,User user2){
        userMapper.updateById(user1);
        userMapper.updateById(user2);
    };

    @Override
    public Page<User> query_users(String number, String username, String academy,String permission, Integer PageNum) {

        Page<User> page=new Page(PageNum,5);

        QueryWrapper<User> Wrapper = new QueryWrapper<>();
        if (number!=null){
            Wrapper.like("anumber",number);
        }
        if (!StringUtils.isEmpty(username)){
            Wrapper.like("username",username);
        }
        if (!StringUtils.isEmpty(academy)){
            Wrapper.like("academy",academy);
        }
        Wrapper.lt("permission",permission);
        return userMapper.selectPage(page,Wrapper);
    }

    @Override
    public Page<User> query_admins(String number, String username, String role,String permission, Integer PageNum) {

        Page<User> page=new Page(PageNum,5);

        QueryWrapper<User> Wrapper = new QueryWrapper<>();
        if (number!=null){
            Wrapper.like("anumber",number);
        }
        if (!StringUtils.isEmpty(username)){
            Wrapper.like("username",username);
        }
        if (!StringUtils.isEmpty(role)){
            Wrapper.like("role",role);
        }
        Wrapper.ge("permission",1);
        Wrapper.lt("permission",permission);
        return userMapper.selectPage(page,Wrapper);
    }

    @Override
    public User select_one(Integer userid) {
        return userMapper.selectById(userid);
    }

    @Override
    public Page<User> selectPage(int pageNumber, int permission,int permission_two) {
        //第二个参数size表示每页显示5条
        Page<User> page=new Page(pageNumber,5);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.ge("permission",permission_two);
        //lt表示少于
        wrapper.lt("permission",permission);
        return userMapper.selectPage(page, wrapper);
    }

    @Override
    public Page<User> selectPage(int pageNumber, int size,int permission,int permission_two) {
        //第二个参数size表示每页显示几条
        Page<User> page=new Page(pageNumber,size);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.ge("permission",permission_two);
        //lt表示少于
        wrapper.lt("permission",permission);
        return userMapper.selectPage(page, wrapper);
    }
}
